package org.travel.app.controller.manage;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.travel.app.common.vo.ResultVO;
import org.travel.app.controller.BaseController;
import org.travel.app.entity.spots.SpotsShopTicket;
import org.travel.app.entity.spots.SpotsTicketPackage;
import org.travel.app.entity.spots.SpotsTickets;
import org.travel.app.entity.ticket.TicketDiscount;
import org.travel.app.entity.ticket.TicketPackages;
import org.travel.app.entity.ticket.UserTicketDiscount;
import org.travel.app.service.impl.TicketServiceImpl;

import java.util.ArrayList;

/**
 * @author nmm
 * @date 2024-12-2024/12/29
 * @deriction njm......
 */
@RestController
@RequestMapping("/manageTicket")
@RequiredArgsConstructor
public class ManageTicketController extends BaseController {
    private final TicketServiceImpl ticketService;

    /**
     * 获取景点优惠券
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getTicketsDiscountList")
    public ResultVO getTicketList(@RequestParam("page") int pageNum, @RequestParam("limit") int pageSize) {
        PageInfo<TicketDiscount> ticketDiscountsList = ticketService.getTicketDiscountsList(pageNum, pageSize);
        return  success(ticketDiscountsList);
    }

    /**
     * 获取景点在售商品
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getShopTicketsList")
    public ResultVO getShopTicketsList(@RequestParam("page") int pageNum, @RequestParam("limit") int pageSize) {
        PageInfo<SpotsShopTicket> spotsShopTicketList = ticketService.getSpotsShopTicketList(pageNum, pageSize);
        return  success(spotsShopTicketList);
    }

    /**
     * 获取景点套餐
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getSpotsPackageList")
    public ResultVO getSpotsPackageList(@RequestParam("page") int pageNum, @RequestParam("limit") int pageSize) {
        PageInfo<TicketPackages> spotsPackage = ticketService.getSpotsPackage(pageNum, pageSize);
        return  success(spotsPackage);
    }


    /**
     * 获取景点套餐用于添加优惠券时显示
     * @return
     */
    @GetMapping("/getSpotsPackages")
    public ResultVO getSpotsPackages() {
        return  success(ticketService.getSpotsPackages());
    }


    /***
     * 添加优惠券
     * @param ticketDiscount 优惠券信息
     * @return
     */
    @PostMapping("/addTicketDiscount")
    public ResultVO addTicketDiscount(@ModelAttribute UserTicketDiscount ticketDiscount) {
        ticketService.addTicketDiscount(ticketDiscount);
        return  success();
    }

    /**
     * 添加景点票
     * @param spotsTickets
     * @return
     */
    @PostMapping("/addSpotsTicket")
    public ResultVO addSpotsTicket(@ModelAttribute SpotsTickets spotsTickets) {
        try {
            ticketService.addSpotsTicket(spotsTickets);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return success();
    }


    /**
     * 添加景点套餐
     *
     * @param ticketPackages
     * @return
     */
    @PostMapping("/addTicketPackage")
    public ResultVO addTicketPackage(@ModelAttribute TicketPackages ticketPackages) {
        try {
            ticketService.addTicketPackage(ticketPackages);
        } catch (Exception e) {
            throw new RuntimeException("添加套餐失败");
        }
        return success(ticketPackages);
    }

    /**
     * 添加景点套餐关联
     * @param spotsTicketPackage
     * @return
     */
    @PostMapping(value = "/addSpotsTicketPackage/{ticketNum}/{packageId}/{spotsId}",consumes = "application/json")
    public ResultVO addSpotsTicketPackage(@RequestBody ArrayList<SpotsTicketPackage> spotsTicketPackage,@PathVariable("ticketNum") int ticketNum,@PathVariable("packageId") int packageId,@PathVariable("spotsId") int[] spotsId) {
        try {
            ticketService.addSpotsTicketPackage(spotsTicketPackage,ticketNum,packageId);
              //添加套餐
            ticketService.addPackSpots(spotsId,packageId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return success();
    }


    /**
     * 删除景点套餐
     * @param packageId
     * @return
     */
    @PostMapping("/delTicketPackage/{packageId}")
    public ResultVO delTicketPackage(@PathVariable("packageId") int packageId) {
        try {
            ticketService.delTicketPackage(packageId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return success();
    }

}